package cn.mulanbay.admin.api.persistent.service;

import cn.mulanbay.common.bean.PageSearch;
import cn.mulanbay.common.exception.PersistentException;
import cn.mulanbay.common.util.StringUtil;
import cn.mulanbay.persistent.entity.LoginLog;
import cn.mulanbay.persistent.entity.SystemMonitorUser;
import cn.mulanbay.persistent.entity.User;
import cn.mulanbay.persistent.entity.UserSetting;
import cn.mulanbay.persistent.enums.MonitorBussType;
import cn.mulanbay.persistent.mapper.LoginLogMapper;
import cn.mulanbay.persistent.mapper.SystemMonitorUserMapper;
import cn.mulanbay.persistent.mapper.UserMapper;
import cn.mulanbay.persistent.mapper.UserSettingMapper;
import cn.mulanbay.persistent.query.QueryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserSettingMapper userSettingMapper;

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Autowired
    private SystemMonitorUserMapper systemMonitorUserMapper;

    /**
     * 获取用户列表
     *
     * @param us
     * @return
     */
    public Page<User> getUserList(PageSearch us){
        Page<User> pp = new Page<>(us.getPage(),us.getPageSize(),us.isNeedTotal());
        QueryWrapper qw = QueryBuilder.build(us);
        pp.addOrder(OrderItem.desc("last_modify_time"));
        pp = baseMapper.selectPage(pp,qw);
        return pp;
    }

    /**
     * 根据用户名或者手机号查询用户信息
     * @param name
     * @return
     */
    public User getUserByUsernameOrPhone(String name){
        return userMapper.getUserByUsernameOrPhone(name);
    }

    /**
     * 更新登录信息
     *
     * @param user
     * @param ll
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void updateLogin(User user, LoginLog ll){
        userMapper.updateById(user);
        loginLogMapper.insert(ll);
    }

    /**
     * 新增用户
     *
     * @param user
     * @param us
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void createUser(User user, UserSetting us) {
        userMapper.insert(user);
        us.setUserId(user.getId());
        userSettingMapper.insert(us);
    }

    /**
     * 修改用户
     *
     * @param user
     * @param us
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void updateUser(User user, UserSetting us) {
        userMapper.updateById(user);
        userSettingMapper.updateById(us);
    }

    public UserSetting getUserSetting(Long userId) {
        return userSettingMapper.getByUserId(userId);
    }

    public List<SystemMonitorUser> getSystemMonitorUserList(Long userId){
        return systemMonitorUserMapper.getListByUserId(userId);
    }

    /**
     * 保存用户监控配置
     *
     * @param userId
     * @param bussTypes
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void saveUserSystemMonitor(Long userId, String bussTypes) {
        systemMonitorUserMapper.deleteByUserId(userId);
        if (StringUtil.isNotEmpty(bussTypes)) {
            String[] ids = bussTypes.split(",");
            for (String s : ids) {
                SystemMonitorUser rf = new SystemMonitorUser();
                rf.setUserId(userId);
                MonitorBussType mbt = MonitorBussType.getMonitorBussType(Integer.valueOf(s));
                rf.setBussType(mbt);
                rf.setCreatedTime(LocalDateTime.now());
                rf.setSmsNotify(true);
                rf.setSysMsgNotify(true);
                rf.setWxNotify(true);
                systemMonitorUserMapper.insert(rf);
            }
        }
    }
}
